package com.dq.service.finance.impl;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.BasePayQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.vo.Vo;
import com.dq.domain.base.AbstractEntity;
import com.dq.domain.base.impl.BaseDaoImpl;

import com.dq.domain.org.Org;
import com.dq.domain.record.BasePaySetRecord;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.finance.BasePaySetRecordService;
import com.dq.service.org.OrgService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author luchangtong
 * @Date 2019/12/21 17:03
 */
@Service
public class BasePaySetRecordServiceImpl  extends BaseRepositories implements BasePaySetRecordService {
    @Autowired
    private OrgService orgService;
    @Override
    public Vo list(BasePayQuery basePayQuery) throws AdminException {
        basePayQuery.format();
        Map<String, Object> query = basePayQuery.parseMap();
        if(basePayQuery.getOrgId()!=null){
            query.put("section_id",basePayQuery.getOrgId());
            query.remove("orgId");
        }else {
            // 只显示负责门店信息
            List<Org> orgList = orgService.getListAllByUser();
            if(orgList == null || orgList.size() <= 0) {
                throw new AdminException("此账号无负责门店");
            }
            query.put("section" + BaseDaoImpl.inSqlStr, orgList);
        }
        if(StringUtils.isNotEmpty(basePayQuery.getUserNo())){
            query.put("cardNo"+ BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(basePayQuery.getUserNo()));
            query.remove("userNo");
        }
        if(StringUtils.isNotEmpty(basePayQuery.getUserRealName())){
            query.put("userRealcName"+BaseDaoImpl.likeSqlStr,BaseAdminQuery.likeSql(basePayQuery.getUserRealName()));
            query.remove("userRealName");
        }
        if(basePayQuery.getBeginTime()!=null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr,basePayQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(basePayQuery.getEndTime()!=null){
            query.put("createTime"+BaseDaoImpl.lteSqlStr,basePayQuery.getEndTime());
            query.remove("endTime");
        }
        List<StaffUser> basePaySetRecords = dao.find(dao.query2Hql(StaffUser.class,query),query);
        List<StaffUser.PcVo> list = new ArrayList<>();
        basePaySetRecords.forEach(staffUser -> {
            list.add(staffUser.toPcVo());
        });
        Long count = dao.count(dao.count2Hql(StaffUser.class,query),query);
        PageHelper.startPage(basePayQuery.getPage(),basePayQuery.getSize());
        PageInfo pageInfo = new PageInfo(list);
        pageInfo.setTotal(count);
        return new Vo(pageInfo);
    }

    @Override
    public Vo add(BigDecimal baseSalary, Long orgId, Long staffUserId)  throws AdminException{

        addRecord(baseSalary, orgId, staffUserId);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo edit(BigDecimal baseSalary, Long orgId, Long staffUserId) throws AdminException {
        addRecord(baseSalary, orgId, staffUserId);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo delete(Long id)  throws AdminException{
        StaffUser staffUser = dao.get(StaffUser.class,id);
        staffUser.setBaseSalary(BigDecimal.ZERO);
        staffUserRepository.save(staffUser);
        return new Vo(HttpStatus.OK);
    }

    public void addRecord(BigDecimal baseSalary, Long orgId, Long staffUserId)throws AdminException{
        SysSection org = dao.get(SysSection.class,orgId);
        if(org == null){
            throw new AdminException("门店信息异常");
        }
        SysUser user = dao.get(SysUser.class,staffUserId);
        if(user == null){
            throw new AdminException("技师信息异常");
        }
        StaffUser staffUser = (StaffUser) user;
        staffUser.setBaseSalary(baseSalary);
        staffUserRepository.save(staffUser);
        BasePaySetRecord basePaySetRecord = new BasePaySetRecord();
        basePaySetRecord.setVersion(Constant.VERSION_VALUE);
        basePaySetRecord.setDeleted(false);
        basePaySetRecord.setCreateTime(System.currentTimeMillis());
        basePaySetRecord.setLastModifyTime(System.currentTimeMillis());
        SysUser sysUser = shiroService.getUser();
        basePaySetRecord.setCreateUser(sysUser);
        basePaySetRecord.setUpdateUser(sysUser);
        basePaySetRecord.setBasePayNo(AbstractEntity.generyDatetimeAndRandomNum(Constant.ORDERS_SUFF_COUNT));
        basePaySetRecord.setOrg(org);
        basePaySetRecord.setSysUser(user);
        basePaySetRecordRepository.save(basePaySetRecord);
    }
}
